package bdd.daos.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import bdd.beans.DBLink;
import bdd.beans.Salle;
import bdd.daos.DAOSalle;

public class DAOSalleImpl implements DAOSalle {
	private DBLink dblink;
	
	public DAOSalleImpl() {
		super();
		this.dblink = new DBLink();
	}
	
	@Override
	public Salle save(Salle o) {
		try {
			this.dblink.connexion();
			String s = "INSERT INTO SALLE (CAPACITE, LIEU)" +
				" VALUES (" + o.getCapacite() + ", '" + o.getLieu() + "')";
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			this.dblink.getPst().executeQuery();
			DAOOutils outil = new DAOOutils();
			o.setNumsalle(outil.findMaxId("SALLE", "NUMSALLE"));
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return o;
	}

	@Override
	public Salle find(Integer numSalle) {
		Salle retour = new Salle();
		retour.setNumsalle(numSalle);
		try {
			this.dblink.connexion();
			String s = "SELECT * FROM SALLE WHERE NUMSALLE = " + numSalle;
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				retour.setNumsalle(rs.getInt("NUMSALLE"));
				retour.setCapacite(rs.getInt("CAPACITE"));
				retour.setLieu(rs.getString("LIEU"));
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
		return retour;
	}

	@Override
	public List<Salle> findAll() {
		List<Salle> listeRetour = new ArrayList();
		try {
			this.dblink.connexion();
			this.dblink.setPst(this.dblink.getConnection().prepareStatement("SELECT * FROM SALLE"));
			ResultSet rs = this.dblink.getPst().executeQuery();
			while(rs.next()) {
				Salle retour = new Salle();
				retour.setNumsalle(rs.getInt("NUMSALLE"));
				retour.setCapacite(rs.getInt("CAPACITE"));
				retour.setLieu(rs.getString("LIEU"));
				listeRetour.add(retour);
			}
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally {
			this.dblink.deconnexion();
		}
		return listeRetour;
	}

	@Override
	public void update(Salle o) {
		try {
			this.dblink.connexion();
			String s = "UPDATE SALLE SET CAPACITE=" + o.getCapacite() + ", LIEU='"+ o.getLieu() + "' WHERE NUMSALLE=" + o.getNumsalle();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally {
			this.dblink.deconnexion();
		}
	}

	@Override
	public void delete(Salle o) {
		try {
			this.dblink.connexion();
			String s = "DELETE FROM SALLE WHERE NUMSALLE=" + o.getNumsalle();
			
			this.dblink.setPst(this.dblink.getConnection().prepareStatement(s));
			this.dblink.getPst().executeQuery();
		}
		catch(SQLException se) {
			System.out.println("Erreur de requete : " + se);
		}
		finally{
			this.dblink.deconnexion();
		}
	}

}
